﻿using Dm;
using LPLS.DBEntities.ConfigModels.AlarmModels.AlarmVarableCodeRuleConfigModels;
using LPLS.DBEntities.DataCollectionModels;
using LPLS.DBInterface.DataCollectionModels;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCollectionVarConfig.ViewModels
{
    /// <summary>
    ///  数据采集配置
    /// </summary>
    public  class DataCollectionMatePropertyViewModel : BindableBase
    {
        private readonly IDataCollectionMatePropertyService _dataCollectionMatePropertyService;



        public DataCollectionMatePropertyViewModel(IDataCollectionMatePropertyService dataCollectionMatePropertyService) { 
        
         _dataCollectionMatePropertyService = dataCollectionMatePropertyService;
            LoadData();
            AddCommand = new DelegateCommand(AddData);
            UpdateCommand=new DelegateCommand (UpdateData);
        }



        private ObservableCollection<DataCollectionMateProperty> _Propertys;

        /// <summary>
        /// 属性字段集合
        /// </summary>
        public ObservableCollection<DataCollectionMateProperty> Propertys
        {
            get { return _Propertys; }
            set { 

                SetProperty( ref _Propertys,value );
            
            }
        }






        private DataCollectionMateProperty _SelectModel;
        /// <summary>
        /// 选中的Model
        /// </summary>
        public DataCollectionMateProperty SelectModel
        {
            get { return _SelectModel; }
            set { 
            
            SetProperty( ref _SelectModel, value );

                if (_SelectModel != null) { 
                
                   this.SelectModel_PropertyCode = _SelectModel.PropertyCode;
                    this.SelectModel_PropertyName = _SelectModel.PropertyName;
                
                
                }


            }
        }


        private string _SelectModel_PropertyName;

       /// <summary>
       /// 选中的名称
       /// </summary>
        public string SelectModel_PropertyName
        {
            get { return _SelectModel_PropertyName; }
            set { 
                
                SetProperty(  ref _SelectModel_PropertyName, value );
                }
        }


        private string _SelectModel_PropertyCode;
        /// <summary>
        /// 选中的属性Code
        /// </summary>
        public string SelectModel_PropertyCode
        {
            get { return _SelectModel_PropertyCode; }
            set { SetProperty(ref _SelectModel_PropertyCode, value); }
        }

        /// <summary>
        /// 加载数据项
        /// </summary>

        private void LoadData() 
        { 
        
            var data = _dataCollectionMatePropertyService.GetData();

            this.Propertys = new ObservableCollection<DataCollectionMateProperty>(data);


        }

        private void AddData() 
        { 
        
           var oldpropertycode=this.Propertys.Where(e=>e.PropertyCode==this.SelectModel_PropertyCode)?.FirstOrDefault();

            if (oldpropertycode != null) {

                Message = "该属性Code已经存在请重试";
                return;
            
            }
            Guid id = Guid.NewGuid();

            DataCollectionMateProperty model = new DataCollectionMateProperty();
            model.ID = id;
            model.PropertyName = this.SelectModel_PropertyName;
            model.PropertyCode = this.SelectModel_PropertyCode;
           var  res=   _dataCollectionMatePropertyService.AddData(model);

            if (res>0)
            {
                Message = "添加成功";
            }
            LoadData();
        }


        private void UpdateData() 
        {
            var id = this.SelectModel.ID;
            this.SelectModel.PropertyName = this.SelectModel_PropertyName;
            this.SelectModel.PropertyCode = this.SelectModel_PropertyCode;
           var res=   _dataCollectionMatePropertyService.Updateddata(SelectModel);
            if (res > 0)
            {

                Message = "修改成功";
            }
            else 
            {

                Message = "修改失败";
            }
            LoadData();
        }
        private string _Message;
        public string Message
        {
            get { return _Message; }
            set {
                SetProperty(ref _Message, value);
        }
        }
        public DelegateCommand AddCommand { get; }

        public DelegateCommand UpdateCommand { get; }
    }
}
